﻿using System.Dynamic;
using blog.Entity;
using blog.Model.Dto;
using blog.Model.Oo;
using blog.Model.Vo;
using blog.Service;
using Furion.ClayObject.Extensions;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace blog.web.Controllers.Back
{
    [ApiDescriptionSettings("后台", Tag = "用户")]
    [Route("api/back/[controller]")]
    public class UserController : IDynamicApiController
    {
        IUserService _userService;
        IConfiguration _configuration;        
        private IHttpContextAccessor _accessor;



        public UserController(IUserService userService, IConfiguration configuration, IHttpContextAccessor accessor)
        {
            _userService = userService;
            _configuration = configuration;
            _accessor = accessor;
        }

        


        [Route("")]
        public async Task<AjaxResult> Get([FromQuery]ListModel param,[FromQuery]SearchKey key)
        {
            return await _userService.Select(param,key);
        }
        
        [Route("")]
        public async Task<AjaxResult> Delete(List<long> id)
        {
            return await _userService.Delete(id);
        }
        
        [Route("")]
        public async Task<AjaxResult> Insert(SysUser user)
        {
            return await _userService.Insert(user);
        }

        [Route("")]
        public async Task<AjaxResult> Update(SysUser user)
        {
            return await _userService.Update(user);
        }
        
        
        
        [AllowAnonymous]
        public async Task<AjaxResult> DoLogin(LoginModel param)
        {
            var jwtOpt = _configuration.GetSection("JwtOpt").Get<JwtOpt>();
            param.jwtOpt = jwtOpt;
            string remoteIpAddress = _accessor.HttpContext.Connection.RemoteIpAddress.ToString();
            return await _userService.Validate(param,remoteIpAddress);
        }

        [HttpPut]
        public async Task<AjaxResult> ChangeStatus(SysUser user)
        {
            return await _userService.ChangeStatus(user);
        }
        
        [HttpPut]
        public async Task<AjaxResult> ChangePwd(SysUser user)
        {
            return await _userService.ChangePwd(user);
        }

        [HttpGet]
        public async Task<AjaxResult> ListLog([FromQuery] ListModel param, [FromQuery] SearchKey key)
        {
            return await _userService.ListLog(param, key);
        }
        

    }
}
